/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos.ui.fieldview;

import com.google.blockly.model.Field;
import com.google.blockly.model.FieldDropdown;
import com.google.blockly.ohos.ResourceTable;
import com.google.blockly.utils.ImageUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import com.google.blockly.ohos.ui.spinner.NiceSpinner;

import java.util.List;

/**
 * Renders a dropdown field as part of a Block.
 */
public class BasicFieldDropdownView extends NiceSpinner implements FieldView {
    private static final String TAG = "BasicFieldDropdownView";

    private Field.Observer mFieldObserver = new Field.Observer() {
        @Override
        public void onValueChanged(Field field, String oldValue, String newValue) {
            //setSelection(mDropdownField.getSelectedIndex());
        }
    };
    private final DrawTask mDrawTask = new DrawTask() {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            Element bg = ImageUtils.getPatchDrawable(getContext(), ResourceTable.Media_dropdown_background_default);
            if (bg != null) {
                bg.setBounds(new Rect(0, 0, component.getEstimatedWidth(), component.getEstimatedHeight()));
                bg.drawToCanvas(canvas);
            }
        }
    };

    protected FieldDropdown mDropdownField;
    protected int mItemLayout;

    /**
     * Constructs a new {@link BasicFieldDropdownView}.
     *
     * @param context The application's context.
     */
    public BasicFieldDropdownView(Context context) {
        super(context);
        loadAttributes(context);
        addDrawTask(mDrawTask);
    }

    public BasicFieldDropdownView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        loadAttributes(context);
        addDrawTask(mDrawTask);
    }

    private void loadAttributes(Context context) {
        mItemLayout = ResourceTable.Layout_simple_spinner_item;
    }

    @Override
    public void setField(Field field) {
        FieldDropdown dropdownField = (FieldDropdown) field;
        if (mDropdownField == dropdownField) {
            return;
        }

        if (mDropdownField != null) {
            mDropdownField.unregisterObserver(mFieldObserver);
        }
        mDropdownField = dropdownField;
        if (mDropdownField != null) {
            List<String> items = mDropdownField.getDisplayNames();
            setAdapter(new FieldAdapter<String>(getContext(), ResourceTable.Layout_simple_spinner_item, items, this));
            if (items.size() > 0) {
                setSelectedIndex(mDropdownField.getSelectedIndex());
            }

            mDropdownField.registerObserver(mFieldObserver);
        } else {
            setSelectedIndex(0);
        }
    }

    @Override
    public void setSelectedIndex(int position) {
        if (position == getSelectedIndex()) {
            return;
        }
        super.setSelectedIndex(position);
        if (mDropdownField != null) {
            mDropdownField.setSelectedIndex(position);
        }
    }

    @Override
    public Field getField() {
        return mDropdownField;
    }

    @Override
    public void unlinkField() {
        setField(null);
    }
}
